import util from './util.js'

export const SIGN_NO = 'de022ef3d06973c4dae01e41feb42a1b'
export const BASE_URL = 'http://localhost:8002'//后面不能加斜杠
export const BASE_IMG_URL = 'http://localhost:8002'
export const ACCESS_TOKEN = 'de022ef3d06973c4dae01e41feb42a1b'


export const CRMURL = '/erp' //定义引用的模块，主要解决是CRM/ERP
export const SYSMODULE = ['erp'] //定义引用的模块菜单,如：[erp,crm,oa]


//api请求函数
export const myRequest = (url, data, method) => {
	
	return new Promise((resolve, reject) => {
		
		let postdata=data || {};
		postdata.user_token=uni.getStorageSync('user_token') || '';
		postdata.access_token=ACCESS_TOKEN || '';
		
		console.log('请求地址=>'+ url);
		console.log('请求方法=>'+ method);
		//console.log('请求参数=>'+ JSON.stringify(postdata));
		
		uni.request({
			// url: BASE_URL + url,//当起用第三方地址才使用
			url: url,//默认使用h5本地调用
			method: method || 'POST',
			data: postdata || {},
			header: {
				'Content-Type': "application/json",
				'token': uni.getStorageSync('token'),
				'sign-no': SIGN_NO
			},
			success: (result) => {

				console.log('返回状态码=>'+result.statusCode);

				let res = result.data	
								
				if (res.code == 1) {
					resolve(res)
				} else if (res.code == 401) {
					uni.navigateTo({
						url: '/pages/admin/login/login'
					})
				} else if (res.code == 402) {
					//未完善 用户 信息
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success: (res) => {
							uni.navigateTo({
								url: '/pages/profile/mySet/setPersonData/setPersonData'
							})
						}
					})
				} else if (res.code == 405) {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success: (res) => {
						}
					})
					uni.reLaunch({
						url: '/pages/index/examineing'
					})
				} else if (res.code == 1000002 || res.code == 1000005) {
					uni.navigateTo({
						url: '/pages/admin/login/login'
					})
				} else {
					let msg='';
					if(result.statusCode!=200){
						msg='返回状态：'+result.statusCode+' '+result.data;
					}else{
						msg=res.msg;
					}
					
					//返回提示码展示
					uni.showToast({
						title: msg,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				console.error('请求失败', err);
				uni.showToast({
					title: '正在拼命加载中',
					icon: 'none'
				})
				reject(err)
			}
		})
	})
}




// export const myRequest = options => {
// 	return new Promise((resolve, reject) => {
// 		uni.request({
// 			url:BASE_URL + options.url,
// 			method:options.method || 'POST',
// 			data:options.data || {},
// 			header:{
// 				'Content-Type':"application/x-www-form-urlencoded",
// 				'codes':'123456'
// 			},
// 			success:(res)=>{
// 				resolve(res)
// 			},
// 			fail: (err) => {
// 				uni.showToast({
// 					title:'请求接口失败'
// 				})
// 				reject(err)
// 			}
// 		})
// 	})
// }
